package com.mhy.utils.toast;

/**
 * Created By Mahongyin
 * Date    2020/11/20 11:57
 */

import android.animation.LayoutTransition;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.CycleInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.viewexplosion.ExplosionField;
import com.example.administrator.viewexplosion.factory.ExplodeParticleFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by  Rex on 2016/9/9.
 * 类似手游中的无限点击上浮的Toast
 * 使用方法 showMsgAdd(String msg, final Activity context)
 */
public class RexToast {

    private static List<TextView> items = new ArrayList<>();
    private static LinearLayout ll;
    private static ExplosionField explosionField;

    public static void init(Activity context) {
        explosionField= new ExplosionField(context,new ExplodeParticleFactory());// ExplosionField.attach2Window(context);
        ;
//        View view = LayoutInflater.from(context).inflate();
//        当然，如果你想自定义一下这个动画效果，那就必须在代码中自己写了：新建一个LayoutTransition对象，调用setLayoutTransition()方法来为layout设置动画。
        ll = new LinearLayout(context);
        ll.setBackgroundColor(Color.TRANSPARENT);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        ll.setLayoutParams(layoutParams);
        ll.setPadding(0, 500, 0, 0);
        ll.setGravity(Gravity.CENTER_HORIZONTAL);
        ll.setOrientation(LinearLayout.VERTICAL);
        LayoutTransition layoutTransition = new LayoutTransition();
//or, if previously enabled in xml, use getLayoutTransition()

//enable animation
        ll.setLayoutTransition(layoutTransition);


//disable animation
//        setLayoutTransition(null);

        context.getWindow().addContentView(ll, new
                ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

    }

    public static void showMsgAdd(String msg, final Activity context) {
        if (ll == null) {
            init(context);
        }
        final TextView item = new TextView(context);
        item.setGravity(Gravity.CENTER);
        item.setTextColor(Color.parseColor("#EB7A77"));
        item.setText(msg);
        item.setPadding(5, 5, 5, 5);
        item.setTextSize(18);
        Typeface mTypeface = Typeface.createFromAsset(context.getAssets(), "fonts/bygf3500.ttf");
        item.setTypeface(mTypeface);
//        ll.setLayoutTransition();
//        android:animateLayoutChanges="true"

        ll.addView(item);
        final TranslateAnimation animation = new TranslateAnimation(item.getWidth(),
                item.getWidth() + 10, item.getHeight(), item.getHeight());

// 利用 CycleInterpolator 参数 为float 的数  表示 抖动的次数，而抖动的快慢是由 duration 和 CycleInterpolator 的参数的大小 联合确定的
        animation.setInterpolator(new CycleInterpolator(2f));
        animation.setDuration(1000);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                ll.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        explosionField.explode(item);
                        if (ll.getChildCount() > 0) {
                            ll.removeView(item);
//                            ll.postDelayed(this,1800);

                        }
                    }
                }, 1800);
                Log.i("rex", "onAnimationEnd");
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                Log.i("rex", "onAnimationRepeat");
            }
        });
        item.startAnimation(animation);


    }


}